package com.xiaozhen.blog.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.xiaozhen.blog.entity.Comment;
import com.xiaozhen.blog.entity.Event;
import com.xiaozhen.blog.event.EventProducer;
import com.xiaozhen.blog.service.BlogService;
import com.xiaozhen.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;



/**
 * @create 2023-04-27 19:34
 */
@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private EventProducer eventProducer;

    @Autowired
    private BlogService blogService;

    /**
     * 获取评论列表
     *
     * @param blogId
     * @param model
     * @return
     */
    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Long blogId, Model model) {
        model.addAttribute("comments", commentService.listCommentByBlogId(blogId));
        return "blog :: commentList";
    }

    /**
     * 发布评论
     *
     * @param comment
     * @return
     */
    @PreAuthorize("hasAnyAuthority('user','admin')")
    @RequestMapping("/comments")
    public String post(Comment comment) throws JsonProcessingException {
        // 从上下文对象取到用户 id
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userId = authentication.getName();
        // 保存用户 id
        comment.setUserId(Long.valueOf(userId));
        // 保存评论
        commentService.saveComment(comment);



        // 通过获取评论 id 找到 用户 id
        Long parentCommentId = comment.getParentCommentId();
        // 将消息存入 RabbitMQ 消息队列
        Event event = new Event();
        if (parentCommentId != -1){
            Comment parentComment = commentService.getById(parentCommentId);
            Long parentCommentUserId = parentComment.getUserId();
            event.setEntityType(1); // 实体类型（1 为回复）
            event.setUserId(parentCommentUserId); // 将要通知的用户的 id
            event.setEntityId(parentComment.getId()); // 评论 id
            event.setEntityUserId(Long.valueOf(userId)); // 发表评论的用户的 id
            eventProducer.sendCommentMessage(event);
        }else{
            // 获取发布博客的博主 id
            Long blogUserId = blogService.getById(comment.getBlogId()).getUserId();
            event.setEntityType(0); // 实体类型（1 为回复）
            event.setUserId(blogUserId); // 将要通知的用户的 id
            event.setEntityId(comment.getId()); // 评论 id
            event.setEntityUserId(Long.valueOf(userId)); // 发表评论的用户的 id
            eventProducer.sendCommentMessage(event);
        }
        return "redirect:/comments/" + comment.getBlogId();
    }

}
